﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Np.GalleryDownloader.Classes
{
    public class DownloadImage : INotifyPropertyChanged
    {
        string name = string.Empty;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        Uri link;
        public Uri Link
        {
            get { return link; }
            set
            {
                link = value;
                OnPropertyChanged("Link");
            }
        }

        bool isDownloaded = false;
        public bool IsDownloaded
        {
            get { return isDownloaded; }
            set
            {
                isDownloaded = value;
                OnPropertyChanged("IsDownloaded");
            }
        }

        DateTime dateDownloaded;
        public DateTime DateDownloaded
        {
            get { return dateDownloaded; }
            set
            {
                dateDownloaded = value;
                OnPropertyChanged("DateDownloaded");
            }
        }

        private DownloadPage dp;

        public DownloadImage(DownloadPage downloadPage, string link)
        {
            dp = downloadPage;
            string uri = NormalizeUri(link);
            Link = new Uri(uri);
            Name = Link.Segments.LastOrDefault();
        }

        private string NormalizeUri(string link)
        {
            if (link.ToLower().Contains(@"http://"))
            {
                return link;
            }
            else
            {
                if (link.ToLower()[0] == '/')
                {
                    link = string.Format(@"http://{0}{1}", dp.SiteRoot, link);
                }
                else{
                    link = string.Format(@"http://{0}{1}", dp.SiteImageRoot, link);
                    return link;
                }
            }
            return link;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
